package com.sc.nft.task;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.DisassemblyHistoryLogDao;
import com.sc.nft.dao.SecondCollectionDao;
import com.sc.nft.entity.DisassemblyHistoryLog;
import com.sc.nft.entity.SecondCollection;
import com.sc.nft.entity.SecondCollectionConfig;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.service.IncomeService;
import com.sc.nft.service.NewSecondCollectionCommonService;
import com.sc.nft.service.SecondCollectionConfigService;
import com.sc.nft.service.SecondCollectionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class SecondTask {
    private final SecondCollectionService secondCollectionService;
    private final SecondCollectionConfigService secondCollectionConfigService;
    private final DisassemblyHistoryLogDao disassemblyHistoryLogDao;
    private final IncomeService incomeServer;
    private final SecondCollectionDao secondCollectionDao;
    private final NewSecondCollectionCommonService newSecondCollectionCommonService;
    private final DigitalCollectionDao digitalCollectionDao;

    /**
     * 取消
     * @throws Exception
     */
    @XxlJob("offShelfAll")
    public void offShelfAll() throws Exception {

        //查询上架中的品牌孵化商品
        List<SecondCollection> list =  secondCollectionDao.offShelfAllList();
        for (SecondCollection secondCollection : list) {
            //品牌孵化 每日定时下架
            secondCollectionService.offShelf(secondCollection);
        }
    }

    /**
     * 计算每日价格
     *
     * @throws Exception
     */
    @XxlJob("salesPriceUp")
    public void salesPriceUp() throws Exception {
        secondCollectionConfigService.priceUp();
    }


    /**
     * 缓存昨日ID
     * @throws Exception
     */
    @XxlJob("cacheYesterdayId")
    public void cacheYesterdayId()   {
        newSecondCollectionCommonService.cacheMaxId();
    }

    /**
     * 自由市场参考价格
     *
     * @throws Exception
     */
    @XxlJob("freeMarketReference")
    public void freeMarketReference() throws Exception {
        secondCollectionConfigService.freeMarketReference();
    }

    /**
     * 拆配收益发放
     *
     * @throws Exception
     */
    @XxlJob("disassemblyIncome")
    public void disassemblyIncome() throws Exception {
        List<DisassemblyHistoryLog> list = disassemblyHistoryLogDao.findListByIsSettleAccounts(Boolean.FALSE);
        list.forEach(incomeServer::disassemblyIncome);
    }

    /**
     * 共识区价格价格上涨
     *
     * @throws Exception
     */
    @XxlJob("consensusUpPrice")
    public void consensusUpPrice() throws Exception {
        List<Long> ecologyCollectionIdList = digitalCollectionDao.getEcologyCollectionIdList();
        for (Long l : ecologyCollectionIdList) {
            SecondCollectionConfig secondCollectionConfig = secondCollectionConfigService.getByCollectionAndType(l, MarketTypeEnum.FREE_MARKET);
            if(ObjectUtil.isNotNull(secondCollectionConfig)){
                secondCollectionConfigService.consensusUpPrice(secondCollectionConfig);
            }         }
    }

    /**
     * 共识区价格价格下跌
     *
     * @throws Exception
     */
    @XxlJob("consensusDownPrice")
    public void consensusDownPrice() throws Exception {
        List<Long> ecologyCollectionIdList = digitalCollectionDao.getEcologyCollectionIdList();
        for (Long l : ecologyCollectionIdList) {
            SecondCollectionConfig secondCollectionConfig = secondCollectionConfigService.getByCollectionAndType(l, MarketTypeEnum.FREE_MARKET);
            if(ObjectUtil.isNotNull(secondCollectionConfig)){
                secondCollectionConfigService.consensusDownPrice(secondCollectionConfig);
            }         }
    }
}
